<?php
	require "../classes/DB.php";
	if(isset($_POST['respuesta']) && !empty($_POST['respuesta'])){
		$res=$_POST['respuesta'];
		$rres=$_POST['reres'];
		if($rres==$res){
			$user=$_POST['us'];
			$db=new DB();
			$db->query("Select * from usuarios Inner Join recuperacion_usuario 
						on recuperacion_usuario.id_usuario='{$id}' and usuarios.Nombre_Usuario='{$user}'");
			$row=$db->getRow();
			$email=$row['Correo_Electronico'];
			$pwd=$row['Password'];
			$db->close();
			$sender="correo@irenk.com";
			$subject="Password Recover IRENK System";
			$body="Su clave de acceso es: ".$pwd."\n Le recomendamos realizar un cambio.";
			$header = "From: ".$sender." <$senderMail>\n";
			$header .= "Reply-To: ".$sender." <$senderMail>\n";
			if(@mail("".$email,"".$subject,"".$body,"".$header)){
				echo "<script language='javascript'>
							alert('Su clave a sido enviada a su correo exitosamente'); 
							parent.location.replace('../Index.php');
					  </script>";
			}else{
				echo "<script language='javascript'>
							alert('Error en el servidor intentelo nuevamente más tarde'); 
							parent.location.replace('../Index.php');
					  </script>";
			}
		}else{
			echo "<script language='javascript'>
						alert('Su respuesta no coincide'); 
						parent.location.replace('../Index.php');
				  </script>";
		}
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>IRENK</title>
</head>

<body><div style="padding:30px;"></div>
<table style="background:url(../images/indexBack.jpg); background-repeat:repeat-x;" height="400" width="100%" align="center" border="0" cellpadding="0" cellspacing="0">
	<tr>
    	<td colspan="2" height="350">
			<table align="center" border="0" cellpadding="0" cellspacing="0">
            	<tr>
                	<td style="background:url(../images/upperLeftCorner.jpg); background-repeat:no-repeat;" width="35" heigh="46">&nbsp;
                    
                    </td>
                    <td style="background:url(../images/upperSide.jpg); background-repeat:repeat-x;" height="46">&nbsp;
                                        
                    </td>
                    <td style="background:url(../images/upperRightCorner.jpg); background-repeat:no-repeat;" width="35" height="46">&nbsp;
                                        
                    </td>
                </tr>
                <tr >
                	<td style="background:url(../images/LeftSide.jpg); background-repeat:repeat-y;" width="35">&nbsp;</td>
                    <td style="background:url(../images/background.jpg);">
                    <table border="0" cellpadding="1" cellspacing="1">
                    <form method="post" action="recPass.php">
                    	<tr><th colspan="2" style="color:#FFFFFF;" align="center">Recuperaci&oacute;n de Contrase&ntilde;a</th>
                    	<?php
							$user=$_GET["u"];
							$db=new DB();
							$db->query("Select Id_Usuario from usuarios where Nombre_Usuario='{$user}'");
							$row=$db->getRow();
							$us=$row[0];
							$nr=$db->total();
							$db->close();
							if($nr>0){
								$db2=new DB();
								$db2->query("Select Pregunta,Respuesta from recuperacion_usuario where id_usuario='{$us}'");
								$row2=$db2->getRow();
								echo "<tr><th align='right' style='color:#FFFFFF;'>
									  Pregunta secreta:&nbsp;</th>
									  <td align='left' style='color:#FFFFFF;'>{$row2['Pregunta']}</td><tr>";
								echo "<tr><th align='right' style='color:#FFFFFF;'>
									  Respuesta:&nbsp;</th>
									  <td><input type='text' name='respuesta' /></td></tr>";
								echo "<input type='hidden' name='reres' value='{$row2['Respuesta']}' />
									  <input type='hidden' name='us' value='{$user}' />
									  <input type='hidden' name='id' value='{$us}' />";
							}else{
								echo "<script language='javascript'>
										alert('Usuario Inexistente'); 
										parent.location.replace('../Index.php');
								  </script>";
							}
						?>
                        <tr><td colspan="2" align="center"><input type="hidden" name="rres" value="" />
                          <input type="submit" value="Recuperar" />
                          <input type="button" value="Cancelar" onclick="parent.location.replace('http://localhost/IRENK/')" />
                        </td></tr>
                    </div></form>
                    </table>
                    </td>
                    <td style="background:url(../images/rightSide.jpg); background-repeat:repeat-y;" width"35">&nbsp;
                    </td>
                </tr>
                <tr>
                	<td style="background:url(../images/bottomLeftCorner.jpg); background-repeat:no-repeat;" width="35" height="46">&nbsp;
                                    
                    </td>
                    <td style="background:url(../images/bottomSide.jpg); background-repeat:repeat-x;" height="46">&nbsp;
                                  
                    </td>
                    <td style="background:url(../images/bottomRightCorner.jpg); background-repeat:no-repeat;" width="35" height="46">&nbsp;
                    
                    </td>
                </tr>
            </table>
        </td>
  </tr>
</table>
</body>
</html>